using HybridCLR;
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using XFABManager;

namespace XFGameFramework.HybridCLR
{
    public class StartUpModuleRequestHybrid : StartUpModuleRequest
    {
        protected static List<string> loadedProjects = new List<string>();


        internal IEnumerator StartUpHybrid(string projectName,string moduleTypeFullName) 
        {

#if !UNITY_EDITOR
             

            if (!loadedProjects.Contains(projectName))
            {


                // 判断是否按照顺序加载
                TextAsset orders = AssetBundleManager.LoadAsset<TextAsset>(projectName, HybridCLRConst.ASSEMBLY_LOADING_ORDER);
                 

                if (orders != null && !string.IsNullOrEmpty(orders.text))
                {
                    // 按照顺序加载
                    string[] order_dlls = JsonConvert.DeserializeObject<string[]>(orders.text);

                    for (int i = 0; i < order_dlls.Length; i++)
                    {
                        TextAsset text = AssetBundleManager.LoadAsset<TextAsset>(projectName, string.Format("{0}.dll",order_dlls[i]));
                        if (text == null) continue;
                        Assembly.Load(text.bytes);
                        AssetBundleManager.UnloadAsset(text);
                    } 
                }
                else
                {
                    // 按照之前的方式加载 
                    // 加载热更dll资源  
                    Object[] all_dlls = AssetBundleManager.LoadAllAssets(projectName, HybridCLRConst.HOT_UPDATE_DLL_BUNDLE_NAME);

                    if (all_dlls != null)
                    {
                        foreach (var item in all_dlls)
                        {
                            TextAsset asset = item as TextAsset;
                            if (asset == null) continue;
                            Assembly.Load(asset.bytes);
                            AssetBundleManager.UnloadAsset(asset);
                        }
                    }
                }
                 
                // 加载元数据dll
                Object[] objects = AssetBundleManager.LoadAllAssets(projectName, HybridCLRConst.METADATA_DLL_BUNDLE_NAME);

                if (objects != null)
                {
                    foreach (var item in objects)
                    {
                        TextAsset asset = item as TextAsset;
                        if (asset == null) continue;
                        RuntimeApi.LoadMetadataForAOTAssembly(asset.bytes, HomologousImageMode.SuperSet);
                        AssetBundleManager.UnloadAsset(asset);
                    }
                }

                loadedProjects.Add(projectName);
            }

#endif

            StartUpModuleRequest request = ModuleManager.StartUpModule(moduleTypeFullName);


            Module = request.Module;


            while (!request.isDone)
            {
                yield return null;
                this.progress = request.progress; 
            }

            Completed(request.error);

        }

    }

}

